#include "stdafx.h"
#include "B2ContactListener.h"

#include "Entity.h"

namespace ZeEngine
{
    B2ContactListener::B2ContactListener() : m_pEntity1(NULL), m_pEntity2(NULL), m_fStartContact(false)
    {
    }


    B2ContactListener::~B2ContactListener()
    {
    }

    void B2ContactListener::BeginContact(b2Contact* contact)
    {
        m_pEntity1 = static_cast<Entity*>(contact->GetFixtureA()->GetBody()->GetUserData());
        m_pEntity2 = static_cast<Entity*>(contact->GetFixtureB()->GetBody()->GetUserData());
        m_fStartContact = true;

        if (m_pEntity1 && m_pEntity2)
        {
           Notify();
        }
    }

    void B2ContactListener::EndContact(b2Contact* contact)
    {
        m_pEntity1 = static_cast<Entity*>(contact->GetFixtureA()->GetBody()->GetUserData());
        m_pEntity2 = static_cast<Entity*>(contact->GetFixtureB()->GetBody()->GetUserData());
        m_fStartContact = false;

        if (m_pEntity1 && m_pEntity2)
        {
            Notify();
        }
    }

    const Entity& B2ContactListener::GetEntity1() const
    {
        return *m_pEntity1;
    }

    const Entity& B2ContactListener::GetEntity2() const
    {
        return *m_pEntity2;
    }

    bool B2ContactListener::IsStartContact() const
    {
        return m_fStartContact;
    }

    bool B2ContactListener::IsEndContact()const
    {
        return !m_fStartContact;
    }
}
